var UserTutorCenter = app.controller('UserTutorCenter', function($rootScope, $scope, $http,$state,UserTutorCenterData) {
	try{
		$rootScope.showf=false;
		$scope.show=false;
		$rootScope.title = '导师中心';
		$rootScope.tab = 1;
		$scope.aid=0;
		$scope.list1={};
		$scope.list2={};

		$scope.f=0;
		$scope.s=0;

		//退出登录
		$scope.logout = function(e){
			if(e){
				e.stopPropagation();
			}
			try{
				var url=SITE_URL+"index.php?app=usermyapp&act=logout&callback=JSON_CALLBACK";
				$http.jsonp(url).success( function(data) {
					if(data.done){
						window.localStorage.u="";
						window.localStorage.p="";
						window.localStorage.user="";

						$rootScope.istutor = false;
						if ($cordovaChat.ChatConnected()) {
							$cordovaChat.ChatDisConnect();
						}

						$rootScope.go("loginmain");
					}
				});
			}
			catch(e){showcatch(e);}
		};

		//获取用户信息
		$scope.user={};
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
		$http.jsonp(url).success(function(data) {
			$scope.user=data.retval;


			$scope.f=$scope.user.tutor.first_price;
			$scope.s=$scope.user.tutor.step_price;
			//获取咨询价格
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=conprice";
			$http.jsonp(url).success(function(data) {
				if(data.done){

					$scope.list1=data.retval.fp;
					$scope.list2=data.retval.sp;

					if($scope.list1!=null&&$scope.list1.length>0){
						for ( var i = 0; i < $scope.list1.length; i++) {
							if(parseInt($scope.list1[i].d_value)==parseInt($scope.user.tutor.first_price)){
								$scope.list1[i].h=1;
								break;
							}
							if(i==$scope.list1.length-1&&parseInt($scope.list1[i].d_value)!=parseInt($scope.user.tutor.first_price)){
								if($scope.list1!=null&&$scope.list1.length>0){
									var v=0;
									if($scope.list1.length%2!=0){
										v=$scope.list1.length/2;
									}else{
										v=$scope.list1.length/2-1;
									}
									$scope.list1[Math.floor(v)].h=1;
								}else if($scope.list1.length==1){
									$scope.list1[0].h=1;
								}
							}
						}
					}

					if($scope.list2!=null&&$scope.list2.length>0){
						for ( var i = 0; i < $scope.list2.length; i++) {
							if(parseInt($scope.list2[i].d_value)==parseInt($scope.user.tutor.step_price)){
								$scope.list2[i].h=1;
								break;
							}
							if(i==$scope.list2.length-1&&parseInt($scope.list2[i].d_value)!=parseInt($scope.user.tutor.step_price)){
								if($scope.list2!=null&&$scope.list2.length>0){
									var v=0;
									if($scope.list2.length%2!=0){
										v=$scope.list2.length/2;
									}else{
										v=$scope.list2.length/2-1;
									}
									$scope.list2[Math.floor(v)].h=1;
								}else if($scope.list2.length==1){
									$scope.list2[0].h=1;
								}
							}
						}
					}
				}
			});

		});



		//导师手册
		$scope.aid=0;
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=dssc";
		$http.jsonp(url).success(function(data) {
			$scope.aid=data.retval;
		});

		//接收咨询与拒绝咨询
		$scope.conclick=function(){
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=conop&id="+$scope.user.tutor.onwork;
			$http.jsonp(url).success(function(data) {
				if(data.done){
					if($scope.user.tutor.onwork==1){
						$scope.user.tutor.onwork=0;
					}else{
						$scope.user.tutor.onwork=1;
					}
				}
			});
		}

		//fpirce点击
		$scope.fclick=function(s){
			for ( var i = 0; i < $scope.list1.length; i++) {
				if(i==s.$index){
					$scope.list1[i].h=1;

					$scope.f=$scope.list1[i].d_value;
				}else{
					$scope.list1[i].h=0;
				}
			}
		}
		//sprice点击
		$scope.sclick=function(s){
			for ( var i = 0; i < $scope.list2.length; i++) {
				if(i==s.$index){
					$scope.list2[i].h=1;
					$scope.s=$scope.list2[i].d_value;
				}else{
					$scope.list2[i].h=0;
				}
			}
		}

		//修改价格
		$scope.updatep=function(){
			var fp=0;
			var sp=0;
			/*for ( var i = 0; i < $scope.list1.length; i++) {
				if($scope.list1[i].h>0){
					fp=parseInt($scope.list1[i].d_key);
				}
			}
			for ( var i = 0; i < $scope.list2.length; i++) {
				if($scope.list2[i].h>0){
					sp=parseInt($scope.list2[i].d_key);
				}
			}*/

			var type="^[0-9]*[1-9][0-9]*$"; 
			var re = new RegExp(type); 
			if($scope.f.match(re)==null||$scope.s.match(re)==null) { 
				showtoast('请输入大于0的整数价格');
				return;
			} 

			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=updatep&fp="+$scope.f+"&sp="+$scope.s;
			$http.jsonp(url).success(function(data) {
				if(data.done){
					$scope.user.tutor.first_price=$scope.f;
					$scope.user.tutor.step_price=$scope.s;
					$scope.show=false;
				}
			});
		}

	}
	catch (e) {
		showcatch(e);
	}
});
UserTutorCenter.UserTutorCenterData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}